package com.dehui.traders.ui.optioanl;

import android.content.Context;

import com.dehui.traders.bean.BaseResponse;
import com.dehui.traders.bean.OptionalCode;
import com.dehui.traders.bean.OptionalCodeRank;
import com.dehui.traders.config.Preferences;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.ui.optioanl.OptionalStockManagerFragment;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by hqq on 18.4.19.
 */

public class OptionalStockManagerViewModel extends BaseViewModel{

    private OptionalStockManagerFragment fragment;

    public BindingCommand cancleOnClick = new BindingCommand(()->{
        fragment.getActivity().finish();
    });

    public BindingCommand saveOnClick = new BindingCommand(()->{
        save();
    });

    public BindingCommand deleteOnClick = new BindingCommand(()->{
        delete();
    });

    public BindingCommand selectOnClick = new BindingCommand(()->{
         fragment.selectAll();
    });

    private List<OptionalCodeRank> locationList;

    private void delete() {
        List<OptionalCode> stockList = fragment.getStockList();
        String myOpitonCode = Preferences.getMyOpitonCode();
        locationList = new Gson().fromJson(myOpitonCode, new TypeToken<List<OptionalCodeRank>>() {}.getType());
        for (int i = 0; i < stockList.size(); i++){
            OptionalCode oc = stockList.get(i);
            if (oc.isSelect()){
                deleteOnLocation(oc);
                deleteOnNet(oc);
            }
        }
        Preferences.saveMyOpitonCode(new Gson().toJson(locationList));
        fragment.notifyDelete();
    }

    private void deleteOnLocation(OptionalCode oc) {
        for (int i = 0; i < locationList.size(); i++){
            OptionalCodeRank codeRank = locationList.get(i);
            if (oc.getCode().equals(codeRank.code)){
                locationList.remove(i);
                break;
            }
        }
    }

    private void deleteOnNet(OptionalCode oc) {
        Call<BaseResponse<String>> call = NetImpl.deleteCode(oc.getCode(), oc.getMarket());
        call.enqueue(new CallListener<BaseResponse<String>>() {
            @Override
            public void _onResponse(Response<BaseResponse<String>> response) {

            }

            @Override
            public void _onFailure() {

            }
        });
    }

    public void save() {
        List<OptionalCode> stockList = fragment.getStockList();
        List<OptionalCodeRank> codeRanks = new ArrayList<>();
        for (int i = 0; i < stockList.size(); i++){
            OptionalCode oc = stockList.get(i);
            String code = oc.getCode();
            OptionalCodeRank codeRank = new OptionalCodeRank(oc.getName(),code,oc.getMarket(),oc.getStatus());
            codeRanks.add(codeRank);
        }

        String codestr = new Gson().toJson(codeRanks);
        Preferences.saveMyOpitonCode(codestr);
        fragment.getActivity().finish();
    }

    public OptionalStockManagerViewModel(Context context, OptionalStockManagerFragment fragment) {
        super(context);
        this.fragment = fragment;
    }


}
